Compiling Purely Functional Structured Programs

نویسندگان

  • Phil Scott
  • Steven Obua
  • Jacques D. Fleuriot
چکیده

We present a marriage of functional and structured imperative programming that embeds in pure lambda calculus. We describe how we implement the core of this language in a monadic DSL which is structurally equivalent to our intended source language and which, when evaluated, generates pure lambda terms in continuation-passing-style. 1. Pure Functional Programming One of the frequently touted benefits of being purely functional lies in the ease with which one can reason about programs. Pure functions have predictable and deterministic behaviour. Applying the same pure function to the same argument always yields the same result anywhere in code, thus allowing us to reason equationally about a program. But the lack of global variables and mutable state might be an impractical restriction for general purpose programming on conventional operating systems, and so it might be argued that it should be targeted to specific domains, such as the system configuration language Nix [5], where reproducibility of system builds is highly prized. Our own ProofScript [10] language is a new entry into the pure functional programming space, where the chosen domain is the development of large scale mechanical verifications of mathematics and software. Our model here is based on massive user collaboration, with users working within what is effectively a single development environment. In this environment, every mechanically verified theorem must be a piece of reproducible, immutable data, much like a Nix derivation. And so in our programming language, in which we write the code that produces the verified theorems, we seek the same discipline of pure functional programming. 2. Structured Programming Pure functional programming typically emphasises recursion and higher order functions for expressing complex control flow, and this level of abstraction can be intimidating to those coming from imperative backgrounds and more mainstream languages such as Python. However, the common imperative idioms from structured programming are not anathema to the aims of pure functional code. Indeed, the popu∗ This work has been funded by EPSRC Grant EP/L011794/1 larity of Static Single Assignment [4] (SSA) in compiler architectures shows that modelling imperative structured programming in a pure representation is of benefit when it comes to performing reliable optimisations and static analyses of code. ProofScript aims for the best of both worlds, marrying structured programming with a rich functional language whilst maintaining purity. We believe that our language bears a close affinity to the control flow model assumed in SSA, but with additional support for first-class functions and mutual recursion. Syntactically, basic ProofScript is a language with support for lexical scope, looping and assignment, but with the following key restrictions: 1. we do not support mutable cells as values; 2. closures close over the values of variables, not their locations; 3. function bodies can only assign to variableswhose lexical scope is contained in the function body. Rule 1 should not need additional explanation. Being able to create and pass around mutable storage locations means that the language encodes shared mutable state that can cross arbitrary regions of code. Rule 2 rules out situations such as: val x = 0 def get () = x val a = get () x = x + 1 val b = get () assert (a == b) Here, we have lost the referential transparency of the expression get (). The meaning of the function was changed by an imperative update. Rule 3 rules out common idioms from the world of impure functional programming such as the imperative (or object-oriented) counter: val counter = do val i = 0 def get () = i def incr () = i = i + 1 [get, incr]

برای دانلود متن کامل این مقاله و بیش از 32 میلیون مقاله دیگر ابتدا ثبت نام کنید

ثبت نام

اگر عضو سایت هستید لطفا وارد حساب کاربری خود شوید

منابع مشابه

From Functional Logic Programs to Purely Functional Programs Preserving Laziness

Functional logic languages extend the setting of functional programming by non-deterministic choices, free variables and narrowing. Most existing approaches to simulate logic features in functional languages do not preserve laziness, i.e., they can only model strict logic programming like in Prolog. Lazy functional logic programming however, has interesting properties supporting a more declarat...

متن کامل

Using program structure to guide optimization in the presence of first-class functions

Compilers for functional languages such as Standard ML can do a good job compiling programs, especially programs that perform symbolic computation. However, they often do a poor job on programs in a wide range of real-world application domains, such as systems programming and scientific computing. One reason for this is that these compilers are not sensitive to program structure, that is, recur...

متن کامل

Purely Functional Structured Programming

The idea of functional programming has played a big role in shaping today’s landscape of mainstream programming languages. Another concept that dominates the current programming style is Dijkstra’s structured programming. Both concepts have been successfully married, for example in the programming language Scala. This paper proposes how the same can be achieved for structured programming and pu...

متن کامل

Semantical Equivalence of Process Functional and Imperative Programs

Source-to-source transformations play crucial role in weaving multiple aspects of computation in aspect languages. Except that expressing imperative programs in the uniform form of expressions simplifies these transformations, this form is useful from the viewpoint of recognizing different aspects of computation at any level of program structure. In this paper we present the relation between im...

متن کامل

Mobile Haskell: Compiling Lazy Functional Programs for the Java Virtual Machine

This paper shows how lazy functional programs can be made mobile by compiling them for the Java Virtual Machine. The Haskell compiler it describes is based on the h,Gi-machine, which is designed for implementing lazy functional languages on parallel processors. Although this is not the obvious thing to do, it leads to a particularly elegant set of translation rules. Sadly though, the speed of t...

متن کامل

Compiling Tree Transforms to Operate on Packed Representations

When written idiomatically in most programming languages, programs that traverse and construct trees operate over pointer-based data structures, using one heap object per-leaf and per-node. This representation is efficient for random access and shape-changing modifications, but for traversals, such as compiler passes, that process most or all of a tree in bulk, it can be inefficient. In this wo...

متن کامل

ذخیره در منابع من


  با ذخیره ی این منبع در منابع من، دسترسی به آن را برای استفاده های بعدی آسان تر کنید

برای دانلود متن کامل این مقاله و بیش از 32 میلیون مقاله دیگر ابتدا ثبت نام کنید

ثبت نام

اگر عضو سایت هستید لطفا وارد حساب کاربری خود شوید

عنوان ژورنال:
  • CoRR

دوره abs/1703.05227  شماره 

صفحات  -

تاریخ انتشار 2017